<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>直播播放器</title>
    <!-- Video.js核心样式 -->
    <link href="https://cdn.jsdelivr.net/npm/video.js@8.8.0/dist/video-js.min.css" rel="stylesheet">
    <style>
        body {
            margin: 0;
            padding: 0;
            background-color: #000;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        .video-container {
            width: 100%;
            height: 100vh;
            max-width: 1920px;
            max-height: 1080px;
            position: relative;
        }
        #my-player {
            width: 100%;
            height: 100%;
        }
        /* 自定义加载状态 */
        .vjs-loading-spinner {
            border-color: rgba(255, 255, 255, 0.3);
            border-top-color: white;
            animation: spin 1s linear infinite;
        }
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div class="video-container">
        <video id="my-player" class="video-js vjs-default-skin" controls preload="auto" 
               poster="https://picsum.photos/1920/1080?random=1">
            <source src="http://192.168.0.151:7788/live/666/hls.m3u8" type="application/x-mpegURL">
            <p class="vjs-no-js">
                要查看此视频，需要启用JavaScript，并考虑升级到
                <a href="https://videojs.com/" target="_blank">支持HTML5视频</a>的网页浏览器。
            </p>
        </video>
    </div>

    <!-- Video.js核心脚本 -->
    <script src="https://cdn.jsdelivr.net/npm/video.js@8.8.0/dist/video.min.js"></script>
    <!-- HLS支持插件（备用CDN） -->
    <script src="https://cdn.jsdelivr.net/npm/hls.js"></script>

    <script>
        // 初始化播放器
        const player = videojs('my-player');

        // 错误处理函数
        function handlePlayerError(message) {
            player.error({
                code: 4,
                message: message || '播放错误，请检查网络连接或直播地址'
            });
            console.error(message);
        }

        // 检查HLS支持
        function setupHlsPlayer() {
            if (window.Hls) {
                // 使用hls.js
                const hls = new Hls({
                    maxBufferLength: 30,
                    maxMaxBufferLength: 60,
                    startFragPrefetch: true
                });
                
                hls.loadSource('http://192.168.0.151:7788/live/666/hls.m3u8');
                hls.attachMedia(playerTech_.el().querySelector('video'));
                
                // 事件监听
                hls.on(Hls.Events.MANIFEST_PARSED, () => {
                    console.log('HLS清单解析成功');
                    player.play().catch(err => {
                        console.error('自动播放失败:', err);
                    });
                });
                
                // 错误处理
                hls.on(Hls.Events.ERROR, (event, data) => {
                    console.error('HLS错误:', data);
                    if (data.fatal) {
                        switch(data.type) {
                            case Hls.ErrorTypes.NETWORK_ERROR:
                                hls.startLoad();
                                break;
                            case Hls.ErrorTypes.MEDIA_ERROR:
                                hls.recoverMediaError();
                                break;
                            default:
                                handlePlayerError('HLS播放错误: ' + data.details);
                                hls.destroy();
                                break;
                        }
                    }
                });
            } else if (player.canPlayType('application/x-mpegURL')) {
                // 浏览器原生支持HLS
                console.log('浏览器原生支持HLS播放');
            } else {
                handlePlayerError('当前浏览器不支持HLS播放');
            }
        }

        // 检查Video.js是否加载成功
        if (window.videojs) {
            // 初始化播放器设置
            player.ready(() => {
                // 优化性能
                player.tech_.off('mousemove');
                
                // 显示加载状态
                player.on('waiting', () => {
                    player.addClass('vjs-waiting');
                });
                player.on('playing', () => {
                    player.removeClass('vjs-waiting');
                });
                
                // 设置自定义错误提示
                player.on('error', () => {
                    const error = player.error();
                    if (error) {
                        console.error('播放器错误:', error);
                    }
                });
            });
            
            // 检查HLS.js是否加载成功
            if (window.Hls) {
                setupHlsPlayer();
            } else {
                // 如果HLS.js加载失败，尝试从备用CDN加载
                const script = document.createElement('script');
                script.src = 'https://unpkg.com/hls.js@1.4.17/dist/hls.min.js';
                script.onload = setupHlsPlayer;
                script.onerror = () => handlePlayerError('HLS播放库加载失败');
                document.body.appendChild(script);
            }
        } else {
            alert('播放器初始化失败，请刷新页面重试');
        }
    </script>
</body>
</html>